package org.xxpay.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.entity.SettBankAccount;
import org.xxpay.core.entity.SettBankAccountExample;
import org.xxpay.core.service.ISettBankAccountService;
import org.xxpay.service.dao.mapper.SettBankAccountMapper;

import java.util.List;

/**
 * @author: dingzhiwei
 * @date: 17/12/7
 * @description:
 */
@Service
public class SettBankAccountServiceImpl implements ISettBankAccountService {

    @Autowired
    private SettBankAccountMapper settBankAccountMapper;

    @Override
    public List<SettBankAccount> select(int offset, int limit, SettBankAccount settBankAccount) {
        SettBankAccountExample example = new SettBankAccountExample();
        example.setOffset(offset);
        example.setLimit(limit);
        SettBankAccountExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, settBankAccount);
        return settBankAccountMapper.selectByExample(example);
    }

    @Override
    public int count(SettBankAccount settBankAccount) {
        SettBankAccountExample example = new SettBankAccountExample();
        SettBankAccountExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, settBankAccount);
        return settBankAccountMapper.countByExample(example);
    }

    @Override
    public SettBankAccount findById(Long id) {
        return settBankAccountMapper.selectByPrimaryKey(id);
    }

    @Override
    public SettBankAccount findByAccountNo(String accountNo) {
        SettBankAccount settBankAccount = new SettBankAccount();
        settBankAccount.setAccountNo(accountNo);
        return find(settBankAccount);
    }

    @Override
    public SettBankAccount findByInfoIdAndAccountNo(Long infoId, String accountNo) {
        SettBankAccount settBankAccount = new SettBankAccount();
        settBankAccount.setInfoId(infoId);
        settBankAccount.setAccountNo(accountNo);
        return find(settBankAccount);
    }

    @Override
    public SettBankAccount find(SettBankAccount settBankAccount) {
        SettBankAccountExample example = new SettBankAccountExample();
        SettBankAccountExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, settBankAccount);
        List<SettBankAccount> settBankAccountList = settBankAccountMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(settBankAccountList)) return null;
        return settBankAccountList.get(0);
    }

    @Override
    public int add(SettBankAccount settBankAccount) {
        return settBankAccountMapper.insertSelective(settBankAccount);
    }

    @Override
    public int update(SettBankAccount settBankAccount) {
        return settBankAccountMapper.updateByPrimaryKeySelective(settBankAccount);
    }

    @Override
    public int updateByMchId(SettBankAccount updateSettBankAccount, Long infoId) {
        SettBankAccountExample example = new SettBankAccountExample();
        SettBankAccountExample.Criteria criteria = example.createCriteria();
        criteria.andInfoIdEqualTo(infoId);
        return settBankAccountMapper.updateByExampleSelective(updateSettBankAccount, example);
    }

    @Override
    public int delete(Long id) {
        return settBankAccountMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SettBankAccount> getSettBankAccount(SettBankAccount settBankAccount) {
        SettBankAccountExample example = new SettBankAccountExample();
        SettBankAccountExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, settBankAccount);
        return settBankAccountMapper.selectByExample(example);
    }

    void setCriteria(SettBankAccountExample.Criteria criteria, SettBankAccount settBankAccount) {
        if(settBankAccount != null) {
            if(settBankAccount.getInfoId() != null) criteria.andInfoIdEqualTo(settBankAccount.getInfoId());
            if(settBankAccount.getInfoType() != null) criteria.andInfoTypeEqualTo(settBankAccount.getInfoType());
            if(settBankAccount.getId() != null) criteria.andIdEqualTo(settBankAccount.getId());
            if(StringUtils.isNotBlank(settBankAccount.getAccountNo())) criteria.andAccountNoEqualTo(settBankAccount.getAccountNo());
            if(settBankAccount.getIsDefault() != null) criteria.andIsDefaultEqualTo(settBankAccount.getIsDefault());
            if(settBankAccount.getAccountType() != null && settBankAccount.getAccountType() != -99) criteria.andAccountTypeEqualTo(settBankAccount.getAccountType());
        }
    }

}
